package com.scoy.honeymei.activity.home;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.scoy.honeymei.MeApp;
import com.scoy.honeymei.R;
import com.scoy.honeymei.activity.WebActivity;
import com.scoy.honeymei.url.MyUrl;
import com.scoy.honeymei.utils.CheckUtil;
import com.scoy.honeymei.utils.SPHelper;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * ClassName:      com.shanchuang.propertycustomer.dialog
 * Description:
 * Author:         GaoZhan & Administrator
 * UpdateDate:     2019/12/9/0009$ 下午 16:12$
 * UpdateRemark:   更新内容
 */
public class FirstDialog extends DialogFragment {


    @BindView(R.id.dn_title_tv)
    TextView dnTitleTv;
    @BindView(R.id.dn_out_tv)
    Button dnOutTv;
    @BindView(R.id.dn_sure_tv)
    Button dnSureTv;
    @BindView(R.id.tvcontent)
    TextView tvcontent;
    private Unbinder bind;


    public static FirstDialog newInstance() {
        Bundle args = new Bundle();
        FirstDialog fragment = new FirstDialog();
        fragment.setArguments(args);
        return fragment;
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE); //无标题
        View view = inflater.inflate(R.layout.dialogf_notice, null, false);
        bind = ButterKnife.bind(this, view);
        // initWeb(MyUrl.PREFIX_PIC + "/detail/html/weAbout.html?type=7", dnNoticeTv);

        initview();

        return view;
    }

    private void initview() {
        SpannableStringBuilder stringBuilder = new SpannableStringBuilder();

        SpannableString ss0 = new SpannableString("在您使用" + getString(R.string.app_name) + "APP服务前，请认真阅读");
        SpannableString ss1 = new SpannableString("《用户协议》");
        SpannableString ss2 = new SpannableString("和");
        SpannableString ss3 = new SpannableString("《隐私政策》");
        SpannableString ss4 = new SpannableString("全部条款，您同意并接受后方可使用我们的服务");

        ForegroundColorSpan colorSpans0 = new ForegroundColorSpan(getResources().getColor(R.color.colorMainBlue));
        ss1.setSpan(colorSpans0, 0, ss1.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Intent intent = new Intent(getActivity(), WebActivity.class);
                intent.putExtra("title", "用户协议");
                intent.putExtra("webType", -1);
                intent.putExtra("url", MyUrl.PREFIX_PIC + "/detail/html/weAbout.html?type=2");
                startActivity(intent);
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                ds.setUnderlineText(false);
            }
        };
        ss1.setSpan(clickableSpan, 0, ss1.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

        ForegroundColorSpan colorSpans1 = new ForegroundColorSpan(getResources().getColor(R.color.colorMainBlue));
        ss3.setSpan(colorSpans1, 0, ss3.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        ClickableSpan clickableSpan2 = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Intent intent = new Intent(getActivity(), WebActivity.class);
                intent.putExtra("webType", -1);
                intent.putExtra("title", "隐私政策");
                intent.putExtra("url", MyUrl.PREFIX_PIC + "/detail/html/weAbout.html?type=7");
                startActivity(intent);
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                ds.setUnderlineText(false);
            }
        };
        ss3.setSpan(clickableSpan2, 0, ss3.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        //该行和select冲突*/
        tvcontent.setMovementMethod(LinkMovementMethod.getInstance());

        stringBuilder.append(ss0);
        stringBuilder.append(ss1);
        stringBuilder.append(ss2);
        stringBuilder.append(ss3);
        stringBuilder.append(ss4);
        tvcontent.setText(stringBuilder);
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        dialog.setCancelable(false);
        Window dialogWindow = dialog.getWindow();
        if (dialogWindow != null) {
            dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
            dialogWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            dialogWindow.setGravity(Gravity.CENTER);
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            lp.gravity = Gravity.CENTER;
            lp.windowAnimations = android.R.style.Animation_InputMethod;
            dialogWindow.setAttributes(lp);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        bind.unbind();
    }

    @OnClick({R.id.dn_out_tv, R.id.dn_sure_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.dn_out_tv:
                outImp.myOut();
                // dismiss();
                break;
            case R.id.dn_sure_tv:
                SPHelper.applyInt("sure", 1);
                MeApp.initLate();
                dismiss();
                break;
        }
    }

    public interface OutImp {
        void myOut();
    }

    OutImp outImp;

    public void setOutImp(OutImp outImp) {
        this.outImp = outImp;
    }
}
